//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    j: 1,//帧动画初始图片
    isSpeaking: false,//是否正在说话
    voices: [],//音频数组
  },
  onLoad: function () {
  },
  //手指按下
  touchdown: function () {
    let voices = this.data.voices
    let that=this
    let savedFilePath = ''

    console.log("手指按下了...")
    speaking.call(this);
    that.setData({
      isSpeaking: true
    })
    //开始录音
    const recorderManager = wx.getRecorderManager()
    recorderManager.onStart(() => {
      console.log('recorder start')
    })
    recorderManager.onPause(() => {
      console.log('recorder pause')
    })
    recorderManager.onStop((res) => {
      console.log('recorder stop', res)
      const { tempFilePath, fileSize } = res
      wx.saveFile({
        tempFilePath: tempFilePath,
        success: function (res) {
          //持久路径
          //本地文件存储的大小限制为 100M
          savedFilePath = res.savedFilePath
          console.log("savedFilePath: " + savedFilePath)
          voices.push({
            num: that.data.voices.length + 1,
            tempFilePath: savedFilePath,
            time: new Date().valueOf(),
            size: (fileSize / 1024).toFixed(2)
          })
          that.setData({
            voices
          })
        }
      })
      wx.showToast({
          title: '录音成功',
          icon: 'success',
          duration: 1000
        })
    })
    recorderManager.onFrameRecorded((res) => {
      const { frameBuffer } = res
      console.log('frameBuffer.byteLength', frameBuffer.byteLength)
    })

    const options = {
      duration: 10000,
      sampleRate: 16000,
      numberOfChannels: 1,
      format: 'mp3',
      frameSize: 50
    }

    recorderManager.start(options)

  },
  //手指抬起
  touchup: function () {
    console.log("手指抬起了...")
    this.setData({
      isSpeaking: false,
    })
    clearInterval(this.timer)
    wx.stopRecord()
  },
  //点击播放录音
  gotoPlay: function (e) {
    var filePath = e.currentTarget.dataset.key;
    //点击开始播放
    wx.showToast({
      title: '开始播放',
      icon: 'success',
      duration: 1000
    })
    const innerAudioContext = wx.createInnerAudioContext()

    innerAudioContext.autoplay = true
    innerAudioContext.src = filePath
    innerAudioContext.onPlay(() => {
      console.log('开始播放')
    })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })


    // wx.playVoice({
    //   filePath: filePath,
    //   success: function () {
    //     wx.showToast({
    //       title: '播放结束',
    //       icon: 'success',
    //       duration: 1000
    //     })
    //   }
    // })
  },
  upLoad(){
    
    let voices = this.data.voices
    wx.uploadFile({
      url: 'https://nsfi.ebestmobile.com:5000/AI/Recognition/speech/file', //仅为示例，非真实的接口地址
      filePath: voices[0].tempFilePath,
      name: "file",
      formData: {
      },
      success(res) {
        const data = res.data
        //do something
        if (res){
          console.log("成功", data)
        }
      }, fail(err) {
        console.log( err)
      }
    })
  }
})
//麦克风帧动画
function speaking() {
  var _this = this;
  //话筒帧动画
  var i = 1;
  this.timer = setInterval(function () {
    i++;
    i = i % 5;
    _this.setData({
      j: i
    })
  }, 200);
}
